﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Office.Interop.Excel;

class ExcelManager
{
    Application App;

    Microsoft.Office.Interop.Excel.Workbook workbook;

    object missing = System.Reflection.Missing.Value;

    public ExcelManager()
    {
        App = new Application();

        App.DisplayAlerts = false;
    }

    public void Display()
    {
        App.Visible = true;
    }

    public void Display(bool FullScreen)
    {
        App.DisplayFullScreen = FullScreen;
        App.Visible = true;
    }

    public void Open(string FileName)
    {
        string name = System.IO.Directory.GetCurrentDirectory() + "\\" + FileName;

        workbook = App.Workbooks.Open(name, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
    }

    public void New()
    {
        workbook = App.Workbooks.Add(missing);
    }

    public void EditCell(int row, int column, string Value)
    {
        ((workbook.Worksheets[1] as Worksheet).Cells[row, column] as Range).Value2 = Value;
    }

    public void InsertCell(int row, int column)
    {
        ((workbook.Worksheets[1] as Worksheet).Cells[row, column] as Range).Insert(missing, missing);
    }

    public void DeleteCell(int row, int column)
    {
        ((workbook.Worksheets[1] as Worksheet).Cells[row, column] as Range).Delete(missing);
    }

    public void RefreshAll()
    {
        workbook.RefreshAll();
    }

    public void GenerateReport(string Path, string PattertName)
    {
        Open(Path + "\\" + PattertName);

        RefreshAll();

        if (System.IO.File.Exists(PattertName))
        {
            System.IO.File.Delete(PattertName);
        }

        Save(System.IO.Directory.GetCurrentDirectory() + "\\temp\\" + PattertName);
    }

    public void Save(object FileName)
    {
        try
        {
            object missing = System.Reflection.Missing.Value;

            workbook.SaveAs(FileName, missing, missing, missing, missing, missing, XlSaveAsAccessMode.xlShared, missing, missing, missing, missing, missing);
        }
        catch (Exception)
        { }
    }

    public void Close()
    {
        App.Quit();
    }
}

